package sk.xorty.si;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Instantiates context and integrating facilities.
 * <br />Requires:
 * <ul>
 *     <li>Running MySQL with DB 'messages'</li>
 *     <li>Running WS found in /webservice folder of this project</li>
 * </ul>
 */
public class IntegrationMain {

    private IntegrationMain() {}

    public static void main(String[] args) {
        insert();
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("/applicationContext.xml");

        // run declarative integrating
        context.start();
    }

    private static void insert() {
        try {
            System.out.println("PREPARING DATA ...");
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/messages", "root", "");
            ScriptRunner runner = new ScriptRunner(conn, false, true);
            runner.runScript(new BufferedReader(new FileReader("src/main/resources/sql/insert.sql")));
            System.out.println("DATA READY!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
